const Service = require('egg').Service;

// const mock = require('mockjs')
// const { list } = mock.mock({
//     'list|4-6': [{
//         pic:Math.random().toString().substr(2,6),
//         title: '@ctitle',
//         area:'@cword(5,8)',
//         time:'@datetime',
//         name:'@cname',
//         'state|1':['已开团','未开团'],
//     }]
// })

class HomeService extends Service {
    async getList() {
        const { ctx, app } = this

        // list.forEach(item => {
        //     this.app.mysql.insert('homes', item)
        // })

        const { id, keyword, pageIndex, pageSize } = ctx.query;
        let sql = `select * from homes where 1=1 `
        let sqlCount = `select count(id) as total from homes where 1=1 `
        if (id) {
            sql += `and id=${id} `
            sqlCount += `and id=${id} `
        }
        if (keyword) {
            sql += `and title like "%${keyword}%" `
            sqlCount += `and title like "%${keyword}%" `
        }
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex - 1) * pageSize},${pageSize} `
        }
        const data = await app.mysql.query(sql)
        const [{ total }] = await app.mysql.query(sqlCount)
        // return data
        return {
            total,
            data
        }
    }

    //// 直播管理
    async getLive() {
        const { ctx, app } = this

        // list.forEach(item => {
        //     this.app.mysql.insert('lives', item)
        // })

        const { id, keyword, pageIndex, pageSize } = ctx.query;
        let sql = `select * from lives where 1=1 `
        let sqlCount = `select count(id) as total from lives where 1=1 `
        if (id) {
            sql += `and id=${id} `
            sqlCount += `and id=${id} `
        }
        if (keyword) {
            sql += `and title like "%${keyword}%" `
            sqlCount += `and title like "%${keyword}%" `
        }
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex - 1) * pageSize},${pageSize} `
        }
        const data = await app.mysql.query(sql)
        const [{ total }] = await app.mysql.query(sqlCount)
        // return data
        return {
            total,
            data
        }
    }

    //// 团购管理
    async getGrop() {
        const { ctx, app } = this

        // list.forEach(item => {
        //     this.app.mysql.insert('grop', item)
        // })

        const { id, keyword, pageIndex, pageSize } = ctx.query;
        let sql = `select * from grop where 1=1 `
        let sqlCount = `select count(id) as total from grop where 1=1 `
        if (id) {
            sql += `and id=${id} `
            sqlCount += `and id=${id} `
        }
        if (keyword) {
            sql += `and title like "%${keyword}%" `
            sqlCount += `and title like "%${keyword}%" `
        }
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex - 1) * pageSize},${pageSize} `
        }
        const data = await app.mysql.query(sql)
        const [{ total }] = await app.mysql.query(sqlCount)
        // return data
        return {
            total,
            data
        }
    }

    /// 添加数据
    async addList() {
        const { ctx, app } = this
        const data = await app.mysql.insert('homes', { ...ctx.request.body })
        return data
    }

    /// 添加团购数据
    async addGrop() {
        const { ctx, app } = this
        const data = await app.mysql.insert('grop', { ...ctx.request.body })
        return data
    }

    /// 编辑数据
    async editList() {
        const { ctx, app } = this
        const { id } = ctx.request.body
        const data = await app.mysql.update('homes', { ...ctx.request.body, id })
        return data
    }

    /// 编辑团购数据
    async editGrop() {
        const { ctx, app } = this
        const { id } = ctx.request.body
        const data = await app.mysql.update('grop', { ...ctx.request.body, id })
        return data
    }

    /// 删除数据
    async delList() {
        const { ctx, app } = this
        const data = await app.mysql.delete('homes', { id: ctx.params.id })
        return data
    }

    /// 删除直播数据
    async delLive() {
        const { ctx, app } = this
        const data = await app.mysql.delete('lives', { id: ctx.params.id })
        return data
    }

    /// 删除团购数据
    async delGrop() {
        const { ctx, app } = this
        const data = await app.mysql.delete('grop', { id: ctx.params.id })
        return data
    }

    /// 详情页数据
    async detailId() {
        const { ctx, app } = this
        const { id } = ctx.params
        const data = await app.mysql.get('lives', { id })
        return data
    }

    /// 搜索直播数据
    async Search() {
        let { ctx, app } = this;
        let { title } = ctx.query;
        let sql = `select * from lives where title like "%${title}%"`;
        let data = await app.mysql.query(sql);
        return data;
    }

    /// 判断审核数据
    async switchLive() {
        const { ctx, app } = this
        const { type } = ctx.query;
        const data = await this.app.mysql.select('lives', {
            where: { state: type }
        })
        return data
    }
}

module.exports = HomeService;